/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.handler.notify;

import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliPayMessage;
import com.egzosn.pay.common.api.PayMessageHandler;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;
import com.medusa.gruul.common.core.config.Jackson;
import com.medusa.gruul.payment.api.enums.PayType;
import com.medusa.gruul.payment.common.constant.AliConst;
import com.medusa.gruul.payment.common.enums.INotifyResponse;
import com.medusa.gruul.payment.common.enums.NotifyResponse;
import com.medusa.gruul.payment.common.enums.TradeStatus;
import com.medusa.gruul.payment.common.helper.PayHelper;
import com.medusa.gruul.payment.service.MultiPayOrderService;
import com.medusa.gruul.payment.service.MultiPayRefundService;
import com.medusa.gruul.payment.service.MultiPayVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 支付宝支付回调处理器
 * @author 张治保
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AliPayMessageHandler implements PayMessageHandler<AliPayMessage, AliPayService> {

    private final MultiPayOrderService orderPaymentService;
    private final MultiPayVerifyService multiPayVerifyService;
    private final MultiPayRefundService multiPayRefundService;
    /**
     * 处理支付回调消息的处理器接口
     * @param payMessage 支付消息
     * @param context    上下文，如果handler或interceptor之间有信息要传递，可以用这个
     * @param payService 支付服务
     */
    @Override
    public PayOutMessage handle(AliPayMessage payMessage, Map<String, Object> context, AliPayService payService) throws PayErrorException {

        log.info("::支付宝支付::notify::{}", Jackson.writeValueAsString(payMessage));
        String outTradeNo = payMessage.getOutTradeNo();
        Map<String, Object> payMessageDetail = payMessage.getPayMessage();
        /*
         * 退款通知
         */
        if (payMessageDetail.get(AliConst.Refund.REFUND_FEE)!= null){
            log.info("::支付宝支付::notify::{}","退款回调");
            multiPayRefundService.refundNotify(PayType.ALIPAY,payMessage);
            return payService.getPayOutMessage(NotifyResponse.ALI_SUCCESS.getCode(), NotifyResponse.ALI_SUCCESS.getMsg());
        }
        /*
         * 支付结果通知
         */
        log.info("::支付宝支付::notify::{}","支付回调");
        TradeStatus tradeStatus = multiPayVerifyService.verifyPayStatus(PayType.ALIPAY, outTradeNo);
        log.info("::支付宝支付::notify::交易状态:{}",tradeStatus);
        if ( TradeStatus.SUCCESS == tradeStatus){
            orderPaymentService.success(payMessage);
        }
        INotifyResponse response =  PayHelper.notifyResponse(tradeStatus, PayType.ALIPAY);
        log.info("::::::支付回调完毕::::::");
        return payService.getPayOutMessage(response.getCode(), response.getMsg());
    }

}
